package cheme.ProcessModel.Equations;

import java.io.IOException;

import cheme.ProcessModel.ChemePropertyChangeEvent;
import cheme.ProcessModel.IChild;
import cheme.ProcessModel.IInterpretable;
import cheme.ProcessModel.IModelElement;
import cheme.ProcessModel.IPropertyAware;
import cheme.ProcessModel.ModelEnv;
import cheme.ProcessModel.Table.Row;
import cheme.ProcessModel.Visitors.IModelVisitor;

/**
 * This class contains a set of single token fields, if a field is edited such 
 * that it contains more than a single token, it will be paresed into a set of 
 * single token fields.
 * 
 * First step is getting it diplay with a single non-tokenized field.
 * 
 * @author Jonathan Brown
 *
 */

public class Equation extends Row implements IInterpretable
{
	/*
	 * Serialization version identifier. (Set to default for now)
	 */
	
	private static final long serialVersionUID = 1L;
	
	/*
	 * An initial equation is one that contains a single token, which contains only the value of defaultText.
	 */
	public static final String DEFAULT_VALUE = "";
	
	public Equation(IModelElement parent)
	{
		this(parent, DEFAULT_VALUE);
	}
	
	public Equation(IModelElement parent, String text)
	{
		super(parent);
		
		EquationField token = new EquationField(this, text);
		token.setToolTipText("Equation element\nYou can drag and drop this to rearrange the equation.");
		getChildren().add(token);
	}
	
	protected void setInitial(boolean initial)
	{
		isInitial = initial;
		firePropertyChange(P_INITIAL, null, initial);
	}
	
	public boolean isInitial()
	{
		return isInitial;
	}
	
	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
	{
		/*
		 * Reestablish the monitoring of equations since the deserialization
		 * does not maintain these.
		 */

		ModelEnv.getInstanceFor(this).hookModelElement(this);

		for(IChild child : getChildren())
		{
			child.addPropertyChangeListener(this);
		}
	}
	
	/*
	 * Note that this is intercepting the fireing of events due to adding or
	 * removing equation tokens, this method does not respond to events from the
	 * child tokens.
	 */
	@Override
	public void firePropertyChange(ChemePropertyChangeEvent evt)
	{
		boolean fireInterpretEvt = (evt.getPropertyName().equals(IPropertyAware.P_EQUATION_TOKEN) || //change to the text of a token... (really not possible as currently designed)
				evt.getPropertyName().equals(IPropertyAware.P_CHILD) || //change in children set membership--common, this one.
				evt.getPropertyName().equals(IPropertyAware.P_CHILDREN));//ditto
		
		if(true == fireInterpretEvt)
		{
			setInitial(false);
		}
		
		super.firePropertyChange(evt);	
		
		if(true == fireInterpretEvt)
		{
			super.firePropertyChange(
					new ChemePropertyChangeEvent(this, P_INTERPRETABLE, null, this));
		}
	}
	
//	@Override
//	protected void handleChildChange(PropertyChangeEvent evt)
//	{
//		firePropertyChange(IPropertyAware.P_INTERPRETABLE, null, this);
//		super.handleChildChange(evt);
//	}
//	
//	@Override
//	protected void zhandleChildrenChange(PropertyChangeEvent evt)
//	{
//		firePropertyChange(IPropertyAware.P_INTERPRETABLE, null, this);
//		super.handleChildrenChange(evt);
//	}
	
//	@Override
//	public void add(Field child)
//	{
//		setSupressUpdateProcessing(true);
//		super.add(child);
//		setSupressUpdateProcessing(false);
//
////		firePropertyChange(IPropertyAware.P_TEXT, null, this);
//	}
//	
//	@Override
//	public void add(Field child, int index)
//	{
//		setSupressUpdateProcessing(true);
//		super.add(child, index);
//		setSupressUpdateProcessing(false);
//	
////		firePropertyChange(IPropertyAware.P_TEXT, null, this);
//	}
//	
//	@Override
//	public void addAll(List<Field> list)
//	{
//		setSupressUpdateProcessing(true);
//		super.addAll(list);
//		setSupressUpdateProcessing(false);
//
////		firePropertyChange(IPropertyAware.P_TEXT, null, this);
//	}
//	
//	@Override
//	public void addAll(List<Field> list, int index)
//	{
//		setSupressUpdateProcessing(true);
//		super.addAll(list, index);
//		setSupressUpdateProcessing(false);
//
////		firePropertyChange(IPropertyAware.P_TEXT, null, this);
//	}
//	
//	@Override
//	public void remove(Field child)
//	{
//		setSupressUpdateProcessing(true);
//		super.remove(child);
//		setSupressUpdateProcessing(false);
//		
////		firePropertyChange(IPropertyAware.P_TEXT, null, this);
//	}
//	
//	@Override
//	public void replace(Field remove, List<Field> add)
//	{
//		setSupressUpdateProcessing(true);
//		super.replace(remove, add);
//		setSupressUpdateProcessing(false);
//		
////		firePropertyChange(IPropertyAware.P_TEXT, null, this);
//	}
//	
//	@Override
//	protected void handleTextChange(PropertyChangeEvent evt)
//	{
//		super.handleTextChange(evt);
//	}
	
	public Object accept(IModelVisitor ruleChecker)
	{
		return ruleChecker.visit(this);
	}
}
